package com.example.module_home

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.lib_common.view.entity.BannerEntity
import com.example.lib_common.view.entity.NoticeEntity
import com.example.lib_common.view.entity.ServiceEntity
import com.example.lib_common.view.view.BaseMvvmFragment
import com.example.module_home.adapter.HomeBannerAdapter
import com.example.module_home.adapter.HotServiceAdapter
import com.example.module_home.adapter.NoticeListAdapter
import com.example.module_home.databinding.FragmentHome2Binding
import com.example.module_home.vm.HomeViewModel
import com.example.module_home.vm.HousesActivity
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer


class HomeFragment2 : BaseMvvmFragment<FragmentHome2Binding>() {
    override fun getLayoutId(): Int =R.layout.fragment_home2

    private val mViewModel:HomeViewModel by activityViewModels()

    private val keyWords = arrayListOf("热门公告","节日彩蛋","园区新闻")
    private lateinit var noticeListAdapter: NoticeListAdapter
    private val noticeList = mutableListOf<NoticeEntity>()
    private val bannerData = mutableListOf(
        BannerEntity(0,R.drawable.t1,""),
        BannerEntity(1,R.drawable.t2,""),
        BannerEntity(2,R.drawable.t3,""),
    )

    private val hotServiceData = mutableListOf(
        ServiceEntity(R.drawable.park_home,"","车位管理",0),
        ServiceEntity(R.drawable.visitor_home,"","访客管理",1),
        ServiceEntity(R.drawable.repair_home,"","物业保修",2),
        ServiceEntity(R.drawable.sign_home,"","考勤打卡",3),
        ServiceEntity(R.drawable.house_home,"","房源信息",4),
        ServiceEntity(R.drawable.shop_home,"","商家推荐",5),
        ServiceEntity(R.drawable.member_home,"","月卡会员",6),
        ServiceEntity(R.drawable.device_home,"","设备管理",7),
    )

    override fun initViews() {
        super.initViews()
        mDataBinding.bannerV2.run {
            setAdapter(HomeBannerAdapter(bannerData))
            setPageTransformer(AlphaPageTransformer())
            setIndicator(CircleIndicator(this@HomeFragment2.requireContext()))
            addBannerLifecycleObserver(this@HomeFragment2.viewLifecycleOwner) //关联Fragment生命周期
        }

        mDataBinding.rvHotService.run {
            layoutManager =GridLayoutManager(this@HomeFragment2.requireContext(),4)
            adapter = HotServiceAdapter(hotServiceData)
        }

        noticeListAdapter =NoticeListAdapter(noticeList)
        mDataBinding.rvNotice.run {
            layoutManager=LinearLayoutManager(this@HomeFragment2.requireContext(),LinearLayoutManager.VERTICAL,false)
            adapter=noticeListAdapter
        }
        // 点击房源信息进行跳转
        mDataBinding.rvHotService.addOnItemTouchListener(object : androidx.recyclerview.widget.RecyclerView.SimpleOnItemTouchListener() {
            override fun onInterceptTouchEvent(rv: androidx.recyclerview.widget.RecyclerView, e: android.view.MotionEvent): Boolean {
                val childView = rv.findChildViewUnder(e.x, e.y)
                if (childView != null) {
                    val position = rv.getChildAdapterPosition(childView)
                    val serviceEntity = hotServiceData[position]
                    if (serviceEntity.name== "房源信息") {
                        // 在这里添加跳转逻辑，例如：
                        // findNavController().navigate(R.id.action_home_to_house)
                        // 或者启动一个新的Activity
                         startActivity(Intent(requireContext(), HousesActivity::class.java))
                    }
                }
                return false
            }
        })
    }

    override fun loadData() {
        super.loadData()
        mViewModel.loadNoticeData(pageIndex = 1, pageSize = 10)



    }

    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mSuccess.observe(this){
            noticeList.addAll(it)
            noticeListAdapter.notifyDataSetChanged()
        }
    }

}












